package uk.co.kevinlockwood.kol.pages;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import uk.co.kevinlockwood.kol.JavaHelpers;

public class CharSheet extends KoLPage {
	public Integer playerid;
	public Integer level;
	public Integer hpCurr, hpMax;
	public Integer mpCurr, mpMax;
	public Integer mus, musBuffed;
	public Integer myst, mystBuffed;
	public Integer mox, moxBuffed;
	public Integer meat;
	public Integer adventures;
	@Override
	public String toString() {
		StringBuilder s = new StringBuilder("playerid=["+playerid+"] ");
		s.append("level=["+level+"] ");
		s.append("hp=["+hpCurr+"/"+hpMax+"] ");
		s.append("mp=["+mpCurr+"/"+mpMax+"] ");
		s.append("mus=["+musBuffed+" ("+mus+")] ");
		s.append("mys=["+mystBuffed+" ("+myst+")] ");
		s.append("mox=["+moxBuffed+" ("+mox+")] ");
		s.append("meat=["+meat+"] ");
		s.append("adventures=["+adventures+"]");
		return s.toString();
	}

	@Override
	public String pageName() {
		return "charsheet.php";
	}

	@Override
	public void buildFromHtml(List<String> html) {
		List<String> statshtml = new ArrayList<String>();
		for (String line : html) {
			int i = line.indexOf("<tr><td align=right>");
			if (i > 0) {
				int start = 0;
				while (i > 0) {
					String s = line.substring(start,i).replaceAll("<[^>]+>", "");
					statshtml.add(s);
					start = i;
					i = line.indexOf("<tr><td align=right>", start+1);
				}
				break;
			}
		}
		for (String stat : statshtml) {
			int i = stat.indexOf(":");
			if (i < 0) continue;
			String name = stat.substring(0,i).toLowerCase(Locale.US);
			if ("current hit points".equals(name)) {
				hpCurr = JavaHelpers.substringToInt(stat, ":", null);
			} else if ("maximum hit points".equals(name)) {
				hpMax = JavaHelpers.substringToInt(stat, ":", null);
			} else if ("current mana points".equals(name)) {
				mpCurr = JavaHelpers.substringToInt(stat, ":", null);
			} else if ("maximum mana points".equals(name)) {
				mpMax = JavaHelpers.substringToInt(stat, ":", null);
			} else if ("muscle".equals(name)) {
				musBuffed = JavaHelpers.substringToInt(stat, ":", " ");
				mus = JavaHelpers.substringToInt(stat, "base: ", ")");
			} else if ("mysticality".equals(name)) {
				mystBuffed = JavaHelpers.substringToInt(stat, ":", " ");
				myst = JavaHelpers.substringToInt(stat, "base: ", ")");
			} else if ("moxie".equals(name)) {
				moxBuffed = JavaHelpers.substringToInt(stat, ":", " ");
				mox = JavaHelpers.substringToInt(stat, "base: ", ")");
			} else if ("adventures remaining".equals(name)) {
				adventures = JavaHelpers.substringToInt(stat, ":", null);
			} else if ("meat".equals(name)) {
				meat = JavaHelpers.substringToInt(stat, ":", null);
			}
		}
		playerid = JavaHelpers.substringToInt(statshtml.get(0), "(#", ")");
		level = JavaHelpers.substringToInt(statshtml.get(0), "Level ", " ");
	}

}
